package cn.iocoder.yudao.position.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.position.entity.PositionDO;
import cn.iocoder.yudao.position.service.PositionService;
import cn.iocoder.yudao.position.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "APP - 职位-信息")
@RestController
@RequestMapping("/app/position/")
@Validated
public class AppPositionController {

    @Resource
    private PositionService positionService;


    @GetMapping("/get")
    @Operation(summary = "获得职位-信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    @TenantIgnore
    public CommonResult<PositionRespVO> get(@RequestParam("id") Long id) {
        PositionDO positionDO = positionService.get(id);
        return success(BeanUtils.toBean(positionDO, PositionRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "求职端-推荐职位")
    @PermitAll
    @TenantIgnore
    public CommonResult<List<PositionDO>> getList(@Valid PositionPageReqVO pageReqVO) {
        return success(positionService.getList(pageReqVO));
    }


    @GetMapping("/tenant/list")
    @Operation(summary = "推荐公司/企业库")
    @PermitAll
    public CommonResult<List<TentSponseVo>> getTentantList(@Valid TenantRespVO tenantRespVO) {
        return success(positionService.getTentantList(tenantRespVO));
    }

    @PostMapping("/create")
    @TenantIgnore
    @Operation(summary = "创建职位-信息")
    //@PreAuthorize("@ss.hasPermission('position::create')")
    public CommonResult<Long> create(@Valid @RequestBody PositionSaveReqVO createReqVO) {
        return success(positionService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职位-信息")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('position::update')")
    public CommonResult<Boolean> update(@Valid @RequestBody PositionSaveReqVO updateReqVO) {
        positionService.update(updateReqVO);
        return success(true);
    }


    @GetMapping("/list-by-tenant")
    @Operation(summary = "获取企业发布的职位列表")
    @TenantIgnore
    public CommonResult<List<PostTionShortVo>> listByTenant(@RequestParam("tenantId") Long tenantId) {
        List<PostTionShortVo> positionList = positionService.getPositionsByTenantId(tenantId);
        if (CollUtil.isNotEmpty(positionList)) {
            return success(positionList);
        } else {
            return success(new ArrayList<PostTionShortVo>());
        }
    }

}


